package wangjg.test.socks;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Socks5Client {
	Socket socket;
	private InputStream in;
	private OutputStream out;

	public Socks5Client() {
		try {
			socket = new Socket("localhost", 8080);
			in = socket.getInputStream();
			out = socket.getOutputStream();
			out.write(new byte[]{5,1,0});
			this.receiv(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void receiv(final InputStream is){
		Thread readThread = new Thread(){
			public void run(){
				try {
					byte[] buffer = new byte[100];
					int len=0;
					while((len=is.read(buffer))!=-1){
						System.out.println("# len="+len);
						System.out.println("  "+SocksUtil.toHex(buffer,len));
						System.out.println("  "+new String(buffer,0,len));
						System.out.println();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		readThread.start();
	}
	
	public static void main(String[] args) {
		new Socks5Client();
	}
}